<script src="{{ getAssets }}js/bivariatetable/bivariatetable.js" type="text/javascript"></script>
<script type="text/javascript">
var goods_id = '{{.goods_id}}';
var entry_id = '{{.entry_id}}';
var middle_id = '{{.middle_id}}';
var ranges = '{{.ranges}}';
var goods_snd = JSON.parse(ranges);
$(function(){
	$.post('{{ urlfor "ErpEntryController.LensBivariate" }}',{'goods_id':goods_id, 'middle_id': middle_id}, function(res){
		if(res.code == 0) {
			$('#goods-title').html(productGroupName(res.data.goods));	
			setBivariatetable(goods_snd, res.data.middle);
		}
	}, 'json');
	
});

function setBivariatetable(goods_snd, goods_detail) {
	var cyl_min = parseFloat(goods_snd.cyl_min);
	var cyl_max = parseFloat(goods_snd.cyl_max);
	var cyl_step = parseFloat(goods_snd.cyl_step);
	var sph_min =  parseFloat(goods_snd.sph_min);
	var sph_max =  parseFloat(goods_snd.sph_max);
	var sph_step =  parseFloat(goods_snd.sph_step);
	var x  = [];
	var y = [];
	var valueInfo = [];
	var valueJson={};
	if(cyl_min==cyl_max){
		var new_va =  formatnumber(cyl_min, "0.00");
		if(new_va>0){
			new_va = "+"+new_va;
		}else if(new_va==0){
			new_va  = new_va.toString()+".00";
		}
		x.push(new_va);
	}else{
		for(var i = cyl_min;i<=cyl_max;i=i+cyl_step){
			var new_va =  formatnumber(i, "0.00");
			if(new_va>0){
				new_va = "+"+new_va;
			}else if(new_va==0){
				new_va  = new_va.toString()+".00";
			}
			x.push(new_va);
		}
	}
	if(sph_min==sph_max){
		var new_va =  formatnumber(sph_min, "0.00");
		if(new_va>0){
			new_va = "+"+new_va;
		}else if(new_va==0){
			new_va  = new_va.toString()+".00";
		}
		y.push(new_va);
	}else{
		for(var i = sph_min;i<=sph_max;i=i+sph_step){
			var new_va =  formatnumber(i, "0.00");
			if(new_va>0){
				new_va = "+"+new_va;
			}else if(new_va==0){
				new_va  = new_va.toString()+".00";
			}
			y.push(new_va);
		}
	}
	
	if(sph_min<0&&sph_max<=0){
		y = y.sort(function(a,b){
			return b-a;
		});
	}
	if(cyl_min<0&&cyl_max<=0){
		x = x.sort(function(a,b){
			return b-a;
		});
	}
	
	for(var v in goods_detail){
		valueJson={};
		valueJson.sph = goods_detail[v].Snd.sph;
		valueJson.cyl = goods_detail[v].Snd.cyl ? goods_detail[v].Snd.cyl : goods_detail[v].Snd.add;
		valueJson.purchase_num = goods_detail[v].purchase_num
		valueInfo.push(valueJson);
	}
	
	$("#bivariate-table").bivariatetable({
		xaxis: x,
		yaxis: y,
		defaultposition: {cyl: x[0], sph: y[0]},
		defaultcolor: "#aaa",
		defaultbgcolors: "#fff",
		xkey: "cyl",
		ykey: "sph",
        reloads:false,
		valuekey: "purchase_num",
		data: valueInfo,
		onselect: function(callback) {
		}
	});
	mathTotalNum();
}

function mathTotalNum(){
	var totamNum= 0;
	var value = $("#bivariate-table").bivariatetable().getData();
	if(value.length==0){
		$("#totoalnumber").text(0);
		return;
	}
	for(var v in value){
		totamNum = parseInt(value[v].purchase_num)+totamNum;
	}
	$("#totoalnumber").text(totamNum);
}
</script>